//
//  CLCollectModel.m
//  chanlin
//
//  Created by Ryan on 14-12-20.
//  Copyright (c) 2014年 chanlin. All rights reserved.
//

#import "CLCollectModel.h"
#import "IFCoreDataManager.h"
#import "NSManagedObject+Dao.h"
#import "CLList.h"
#import "CLCollect.h"
#import "CLCollect+oper.h"
#import "CLUrl.h"
#import "MobClick.h"

@implementation CLCollectModel

SGR_DEF_SINGLETION(CLCollectModel)

- (void)saveListDic:(NSDictionary *)dic block:(void (^)(BOOL success))block{
    if(!dic){
        if(block){
            sgrSafeMainThread(^{
                block(NO);
            })
        }
        return ;
    }
    
    [[IFCoreDataManager sharedInstance] performBlock:^(NSManagedObjectContext *moc){
        NSString *str=[dic sgrGetStringForKey:@"id"];
        
        CLCollect *collect=[CLCollect managerObjByCreateOrGetWithPredicate:[NSPredicate predicateWithFormat:@"itemId==%@",str]
                                                                       moc:moc];
        
        [collect fillByDic:dic];
        collect.time=[[NSDate date] timeIntervalSince1970];
        [[IFCoreDataManager sharedInstance] saveContext:moc ToPersistentStore:nil];
        if(block){
            sgrSafeMainThread(^{
                block(YES);
            })
        }
        
    }];
    
}



- (void)saveList:(NSString *)itemId block:(void (^)(BOOL success))block{
    
    
    [[IFCoreDataManager sharedInstance] performBlock:^(NSManagedObjectContext *moc) {
        CLList *list=(CLList *)[CLList fetchWithPredict:[NSPredicate predicateWithFormat:@"itemId==%@",itemId]
                                      withMOC:moc];
        if(!list){
            if(block){
            sgrSafeMainThread(^{
                block(NO);
            })
            }
            return ;
        }
        CLCollect *collect=[CLCollect managerObjByCreateOrGetWithPredicate:[NSPredicate predicateWithFormat:@"itemId==%@",itemId]
                                                                       moc:moc];
        
        [collect fillByList:list];
        collect.time=[[NSDate date] timeIntervalSince1970];
        
     
        if(list.subUrls.count>0){
            if(collect.subCurl){
                for(CLUrl *url in collect.subCurl){
                    [moc deleteObject:url];
                }
                NSMutableOrderedSet *set=[NSMutableOrderedSet orderedSet];
                for(CLUrl *str in list.subUrls){
                    CLUrl *url=[CLUrl managerObjWithMoc:moc];
                    url.url=str.url;
                    [set addObject:url];
                    
                }
                collect.subCurl=set;
                
            }
        }
        
        
        [[IFCoreDataManager sharedInstance] saveContext:moc ToPersistentStore:nil];
        if(block){
        sgrSafeMainThread(^{
            block(YES);
        })
        }
        
    }];
    [MobClick event:@"col"
         attributes:@{@"pageId":[@"col_" stringByAppendingString:_safeStr(itemId)]}];
    
}

- (void)checkExitByItemId:(NSString *)itemId block:(void (^)(BOOL success))block{
    [[IFCoreDataManager sharedInstance] performBlock:^(NSManagedObjectContext *moc){
        NSArray *array=[CLCollect fetchAllWithPredict:[NSPredicate predicateWithFormat:@"itemId==%@",itemId]
                                               withMOC:moc];
        BOOL b=array&&[array count]>0;
        if(block){
        sgrSafeMainThread(^{
            block(b);
        })
        }
    }];
}

- (void)deleteItemId:(NSString *)itemId block:(void (^)(BOOL success))block{
    [[IFCoreDataManager sharedInstance] performBlock:^(NSManagedObjectContext *moc){
        CLCollect *collect=(CLCollect *)[CLCollect fetchWithPredict:[NSPredicate predicateWithFormat:@"itemId==%@",itemId]
                                                            withMOC:moc];
        if(collect){
            [moc deleteObject:collect];
             [[IFCoreDataManager sharedInstance] saveContext:moc ToPersistentStore:nil];
            if(block){
            sgrSafeMainThread(^{
                block(YES);
            })
            }
        }else{
            if(block){
            sgrSafeMainThread(^{
                block(NO);
            })
            }
        }
        
    }];
}

@end
